import axios from 'axios';

// axios.defaults.withCredentials = true;

const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    // easy-mock服务挂了，暂时不使用了
    // baseURL: 'https://www.easy-mock.com/mock/592501a391470c0ac1fab128',
    baseURL: 'http://localhost:8080',
    headers: {'Content-Type': 'application/json'},
    // withCredentials: true,
    timeout: 50000
});

service.interceptors.request.use(
    config => {
        // console.log(config.url)
        // console.log(localStorage.getItem('authToken'))
    // 给每个请求都加上token请求头 || config.url === '/checkLogin' && (localStorage.getItem('token') != null)
    if (config.url !== '/userlogin') {
        if (localStorage.getItem('authToken')) {
        //   config.headers.token = localStorage.getItem('token');
          config.headers.AUTH_TOKEN =localStorage.getItem('authToken');
        //   console.log(config.headers.AUTH_TOKEN)
        } else {
          this.$router.push('/');
        }
      }
      return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    response => {
        if (response.status === 200) {
            return response.data;
        } else {
            Promise.reject();
        }
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);


export default service;
